package ynwx.libaray.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import ynwx.libaray.utils.LogUtil;
import ynwx.mylibrary.R;

/**
 * http://blog.csdn.net/yy007h/article/details/50455660
 * Created by Chron on 2017/11/14.
 */

public class BaseFragment extends Fragment {
    private static final String TAG = LogUtil.TAG + BaseFragment.class.getSimpleName();
    private static final boolean D = true;

    protected Toolbar toolbar;
    protected TextView tvToolbarTitle;
    protected TextView tvToolbarRight;
    protected ImageView ivToolbarRight;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        toolbar = initToolbar(view, R.id.toolbar, R.string.app_name);
    }

    protected Toolbar initToolbar(View parent, int toolbarId, int titleId) {
        return initToolbar(parent, toolbarId, getString(titleId));
    }

    protected Toolbar initToolbar(View parent, int toolbarId, String title) {
        AppCompatActivity mAppCompatActivity = (AppCompatActivity) getActivity();
        if (D) LogUtil.i(TAG, "initToolbar mAppCompatActivity:" + mAppCompatActivity);
        Toolbar toolbar = (Toolbar) parent.findViewById(toolbarId);
        mAppCompatActivity.setSupportActionBar(toolbar);
        ActionBar actionBar = mAppCompatActivity.getSupportActionBar();
        if (D) LogUtil.i(TAG, "initToolbar toolbar:" + toolbar);
        tvToolbarTitle = toolbar.findViewById(R.id.toolbar_title);
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
            if (tvToolbarTitle != null) {
                actionBar.setDisplayShowTitleEnabled(false);
                tvToolbarTitle.setText(title);
            }
        }
        tvToolbarRight = toolbar.findViewById(R.id.tv_toolbar_right);
        ivToolbarRight = toolbar.findViewById(R.id.iv_toolbar_right);
        return toolbar;
    }

}
